package com.bwei.topnews.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.VideoView;

import com.bwei.topnews.R;
import com.bwei.topnews.base.MyBaseAdapter;
import com.bwei.topnews.bean.NewsCategoryRoot;
import com.bwei.topnews.utils.CommonUtils;
import com.bwei.topnews.utils.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by qwe on 2016/11/3.
 */
public class CategoryAdapter extends MyBaseAdapter<NewsCategoryRoot.DataBean>{
    /**
     * 普通条目类型
     */
    public static final int TYPE_NORMAL = 0;
    /**
     * 大图片条目类型
     */
    public static final int TYPE_LARGE_PIC = 1;
    /**
     * 没有图片类型
     */
    public static final int TYPE_NO_PIC = 2;
    /**
     * 多图片类型
     */
    public static final int TYPE_MULITY_PIC = 3;
    /*
    视频类型
     */
    public static final int TYPE_VIDEO = 4;
    private final DisplayImageOptions options;


    public CategoryAdapter(List<NewsCategoryRoot.DataBean> list, Context context) {
        super(list, context);
        options = ImageLoaderUtils.initOptions();
    }

    @Override
    public int getItemViewType(int position) {
        // 如果有视频返回视频类型
        if(list.get(position).isHas_video()){
            return TYPE_VIDEO;
            //如果没有图片返回没有图片类型
        }else if(!list.get(position).isHas_image()){
            return TYPE_NO_PIC;
            // 如果装大图片的集合不等于null  并且 集合数量大于0代表有大图片
        }else if(list.get(position).getLarge_image_list()!=null && list.get(position).getLarge_image_list().size()>0){
            return TYPE_LARGE_PIC;
            // 判断放图片的集合不等于空  并且  集合数量大于1，代表有多张图片
        }else if(list.get(position).getImage_list()!=null && list.get(position).getImage_list().size()>1){
            return TYPE_MULITY_PIC;
        }
        return TYPE_NORMAL;
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    protected View initGetView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int itemViewType = getItemViewType(position);
        if(convertView == null){
            holder = new ViewHolder();

            switch (itemViewType){
                case TYPE_NORMAL:
                    convertView = CommonUtils.inflate(R.layout.type_normal_item);
                    holder.iv_news_pic = (ImageView) convertView.findViewById(R.id.iv_news_pic);
                    break;
                case TYPE_LARGE_PIC:
                    convertView = CommonUtils.inflate(R.layout.type_large_pic_item);
                    holder.iv_news_pic = (ImageView) convertView.findViewById(R.id.iv_news_pic);
                    break;
                case TYPE_NO_PIC:
                    convertView = CommonUtils.inflate(R.layout.type_no_pic_item);
                    break;
                case TYPE_MULITY_PIC:
                    convertView = CommonUtils.inflate(R.layout.type_multiple_pic_item);
                    holder.iv_news_pic1 = (ImageView) convertView.findViewById(R.id.iv_news_pic1);
                    holder.iv_news_pic2 = (ImageView) convertView.findViewById(R.id.iv_news_pic2);
                    holder.iv_news_pic3 = (ImageView) convertView.findViewById(R.id.iv_news_pic3);
                    break;
                case TYPE_VIDEO:
                    convertView = CommonUtils.inflate(R.layout.type_video_item);
                    holder.iv_news_pic = (ImageView) convertView.findViewById(R.id.iv_news_pic);
                    holder.iv_news_play_movie = (ImageView) convertView.findViewById(R.id.iv_news_movie_play);
                    holder.vv_new_movie = (VideoView) convertView.findViewById(R.id.vv_news_movie);
                    break;
            }
            holder.tv_news_comment = (TextView) convertView.findViewById(R.id.tv_news_comment);
            holder.tv_news_source = (TextView) convertView.findViewById(R.id.tv_news_source);
            holder.tv_news_time = (TextView) convertView.findViewById(R.id.tv_news_time);
            holder.iv_news_delete = (ImageView) convertView.findViewById(R.id.iv_news_delete);
            holder.tv_news_type = (TextView) convertView.findViewById(R.id.tv_news_type);
            holder.tv_news_title = (TextView) convertView.findViewById(R.id.tv_news_title);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        switch (itemViewType){
            case TYPE_NORMAL:
                ImageLoader.getInstance().displayImage(list.get(position).getMiddle_image().url_list.get(0).url,holder.iv_news_pic,options);
                break;
            case TYPE_LARGE_PIC:
                ImageLoader.getInstance().displayImage(list.get(position).getLarge_image_list().get(0).url,holder.iv_news_pic,options);
                break;
            case TYPE_NO_PIC:
                break;
            case TYPE_MULITY_PIC:
                ImageLoader.getInstance().displayImage(list.get(position).getImage_list().get(0).url,holder.iv_news_pic1,options);
                ImageLoader.getInstance().displayImage(list.get(position).getImage_list().get(1).url,holder.iv_news_pic2,options);
                ImageLoader.getInstance().displayImage(list.get(position).getImage_list().get(2).url,holder.iv_news_pic3,options);
                break;
            case TYPE_VIDEO:
                holder.iv_news_pic.setScaleType(ImageView.ScaleType.CENTER);
                ImageLoader.getInstance().displayImage(list.get(position).getLarge_image_list().get(0).url,holder.iv_news_pic,options);
                break;
        }
        holder.tv_news_comment.setText(list.get(position).getComment_count()+"");
        holder.tv_news_source.setText(list.get(position).getSource());
        holder.tv_news_time.setText(list.get(position).getPublish_time()+"");

        holder.tv_news_type.setText(list.get(position).getArticle_type()+"");
        holder.tv_news_title.setText(list.get(position).getTitle());
        if(list.get(position).isRead()){
            holder.tv_news_title.setTextColor(Color.GRAY);
        }else{
            holder.tv_news_title.setTextColor(Color.BLACK);
        }


        holder.iv_news_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = View.inflate(context,R.layout.popup_uninterest,null);
                Button btn = (Button) view.findViewById(R.id.bt_pop_ensure);
                int[] location = new int[2];
                v.getLocationInWindow(location);
                final PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popupWindow.showAtLocation(v, Gravity.TOP,location[0],location[1]);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        list.remove(position);
                        popupWindow.dismiss();
                        CategoryAdapter.this.notifyDataSetChanged();
                    }
                });
            }
        });

        return convertView;
    }
    class ViewHolder{
        TextView tv_news_title, tv_news_comment, tv_news_source, tv_news_time,
                tv_news_type;
        ImageView iv_news_pic, iv_news_play_movie, iv_news_pic1, iv_news_pic2,
                iv_news_pic3, iv_news_delete;
        VideoView vv_new_movie;
    }
}
